class MilestonesController < InnerApplicationController

  def index
#    @milestones = @project.milestones.all(:include => [:activities, :project], :order => :due)
#    
#    @milestones_completed = @project.milestones.completed.all(:include => [:activities, :project], :order => :due)
#    
#    @milestones_months = @project.milestones.all(:include => [:activities, :project], 
#                                                 :order => :due
#                                                 ).group_by { |milestone| milestone.due.beginning_of_month }
#     
    @late = @project.milestones.late.all(:order => :due)
    
    @scheduled = @project.milestones.pending.all(:order => :due) - @late
    
    @completed = @project.milestones.completed.all(:order => :due)
    
    @unscheduled = @project.milestones.all(:order => :due) - @late - @scheduled - @completed
    
    @upcoming = @project.milestones.pending.all(:include => [:activities, :project],
                                                :order => :due).group_by { |milestone| milestone.due.beginning_of_month }
    #@upcoming = @milestones
    #@upcoming = @upcoming.delete_if {|milestone| !milestone.completed?}
    #@upcoming = @upcoming.group_by  {|milestone| milestone.due.beginning_of_month}
  end

  # GET /milestones/1
  # GET /milestones/1.xml
  def show
    @milestone = @project.milestones.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @milestone }
    end
  end

  # GET /milestones/new
  # GET /milestones/new.xml
  def new
    @milestone = @project.milestones.new
    
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @milestone }
    end
     
  end

  # GET /milestones/1/edit
  def edit
    @milestone = @project.milestones.find(params[:id])
  end

  # POST /milestones
  # POST /milestones.xml
  def create
    @milestone = @project.milestones.new(params[:milestone])

    respond_to do |format|
      if @milestone.save
        flash[:notice] = 'Milestone was successfully created.'
        format.html { redirect_to([@project, @milestone]) }
        format.xml  { render :xml => @milestone, :status => :created, :location => @milestone }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @milestone.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /milestones/1
  # PUT /milestones/1.xml
  def update
    @milestone = @project.milestones.find(params[:id])

    respond_to do |format|
      if @milestone.update_attributes(params[:milestone])
        flash[:notice] = 'Milestone was successfully updated.'
        format.html { redirect_to([@project, @milestone]) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @milestone.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /milestones/1
  # DELETE /milestones/1.xml
  def destroy
    @milestone = @project.milestones.find(params[:id])
    @milestone.destroy

    respond_to do |format|
      format.html { redirect_to(milestones_url) }
      format.xml  { head :ok }
      format.js
    end
  end
end
